package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.Rating;
import javafx.geometry.Orientation;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ControlActions;

public class RatingActions {
    static{
        PropertyFactory.regist(Rating.class, "partialRatingProperty", o -> {
            Rating obj = (Rating) o;
            return obj.partialRatingProperty();
        });
        PropertyFactory.regist(Rating.class, "updateOnHoverProperty", o -> {
            Rating obj = (Rating) o;
            return obj.updateOnHoverProperty();
        });
        PropertyFactory.regist(Rating.class, "maxProperty", o -> {
            Rating obj = (Rating) o;
            return obj.maxProperty();
        });
        PropertyFactory.regist(Rating.class, "orientationProperty", o -> {
            Rating obj = (Rating) o;
            return obj.orientationProperty();
        });
        PropertyFactory.regist(Rating.class, "ratingProperty", o -> {
            Rating obj = (Rating) o;
            return obj.ratingProperty();
        });
    }

    public static void init(Rating node, Thing thing, ActionContext actionContext) {
        ControlActions.init(node, thing, actionContext);

        if (thing.valueExists("max")) {
            node.setMax(thing.getInt("max"));
        }

        if(thing.valueExists("orientation")){
            node.setOrientation(Orientation.valueOf(thing.getStringBlankAsNull("orientation")));
        }

        if (thing.valueExists("partialRating")) {
            node.setPartialRating(thing.getBoolean("partialRating"));
        }

        if (thing.valueExists("rating")) {
            node.setRating(thing.getDouble("rating"));
        }

        if (thing.valueExists("updateOnHover")) {
            node.setUpdateOnHover(thing.getBoolean("updateOnHover"));
        }

    }

    public static Rating create(ActionContext actionContext) {
        Thing self = actionContext.getObject("self");
        Rating node = new Rating();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for (Thing child : self.getChilds()) {
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }
}